<?php
/**
 * Created by PhpStorm.
 * User: kamlyli
 * Date: 2017/7/28
 * Time: 20:35
 */

/**
 * 解密
 */
function message_decrypt($message)
{
    if (empty($message)) {
        return [];
    }
    $split = explode("**", $message);

    if (count($split) == 2) {
        $token = config('params.SECRET_TOKEN_BASE');
        $user_session = json_decode(openssl_decrypt(base64_decode($split[0]), 'AES-256-CBC', $token, false, base64_decode($split[1])), true);

        return $user_session;
    }

    return [];
}

/*
 * 加密
 */
function message_encrypt($message)
{
    if (empty($message)) {
        return '';
    }
    // token base
    $token = config('params.SECRET_TOKEN_BASE');

    $iv = openssl_random_pseudo_bytes(16);
    $encrypted = openssl_encrypt(json_encode($message), 'AES-256-CBC', $token, false, $iv);
    $encrypted = base64_encode($encrypted) . "**" . base64_encode($iv);

    return $encrypted;
}


